package com.ruoyi.sywx.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.sywx.domain.bo.SywxPosterBo;
import com.ruoyi.sywx.domain.vo.SywxPosterVo;
import com.ruoyi.sywx.domain.SywxPoster;
import com.ruoyi.sywx.mapper.SywxPosterMapper;
import com.ruoyi.sywx.service.ISywxPosterService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 发帖人信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-09
 */
@RequiredArgsConstructor
@Service
public class SywxPosterServiceImpl implements ISywxPosterService {

    private final SywxPosterMapper baseMapper;

    /**
     * 查询发帖人信息
     */
    @Override
    public SywxPosterVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询发帖人信息列表
     */
    @Override
    public TableDataInfo<SywxPosterVo> queryPageList(SywxPosterBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SywxPoster> lqw = buildQueryWrapper(bo);
        Page<SywxPosterVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询发帖人信息列表
     */
    @Override
    public List<SywxPosterVo> queryList(SywxPosterBo bo) {
        LambdaQueryWrapper<SywxPoster> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SywxPoster> buildQueryWrapper(SywxPosterBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SywxPoster> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getNetName()), SywxPoster::getNetName, bo.getNetName());
        lqw.like(StringUtils.isNotBlank(bo.getName()), SywxPoster::getName, bo.getName());
        lqw.like(StringUtils.isNotBlank(bo.getIdCard()), SywxPoster::getIdCard, bo.getIdCard());
        lqw.like(StringUtils.isNotBlank(bo.getPhone()), SywxPoster::getPhone, bo.getPhone());
        lqw.like(StringUtils.isNotBlank(bo.getAddress()), SywxPoster::getAddress, bo.getAddress());
        return lqw;
    }

    /**
     * 新增发帖人信息
     */
    @Override
    public Boolean insertByBo(SywxPosterBo bo) {
        SywxPoster add = BeanUtil.toBean(bo, SywxPoster.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改发帖人信息
     */
    @Override
    public Boolean updateByBo(SywxPosterBo bo) {
        SywxPoster update = BeanUtil.toBean(bo, SywxPoster.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SywxPoster entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除发帖人信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
